package com.dyz.leetcode._05_sort;

import java.util.Arrays;

public class _628_maximumProduct {
    public int maximumProduct(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        return Math.max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3]);
    }

    public int maximumProduct1(int[] nums) {
        int f_min = Integer.MAX_VALUE;
        int s_min = Integer.MAX_VALUE;
        int f_max = Integer.MIN_VALUE;
        int s_max = Integer.MIN_VALUE;
        int t_max = Integer.MIN_VALUE;
        for(int num:nums){
            if(num>f_max){
                t_max = s_max;
                s_max = f_max;
                f_max = num;
            }else if(num>s_max){
                t_max = s_max;
                s_max = num;
            }else if(num > t_max){
                t_max = num;
            }

            if(num<f_min){
                s_min = f_min;
                f_min = num;
            }else if(num<s_min){
                s_min = num;
            }
        }
        return Math.max(f_min*s_min*f_max, f_max*s_max*t_max);
    }
}
